package com.example.websocket.utils;


import com.auth0.jwt.JWT;
import com.auth0.jwt.algorithms.Algorithm;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class AppJwtUtil {
    private static final String KEY = "hanfeiyu";
    public static String getJwtToken(int id,String username) {
        HashMap<String, Object> claims = new HashMap<>();
        claims.put("id",id);
        claims.put("username",username);
        String token = JWT.create()
                .withClaim("user",claims)//载荷
                .withExpiresAt(new Date(System.currentTimeMillis()+1000*60*60*12))//过期时间
                .sign(Algorithm.HMAC256(KEY));//算法
        return token;
    }

    public static Map<String,Object> parseToken(String token) {
        return JWT.require(Algorithm.HMAC256(KEY))
                .build()
                .verify(token)
                .getClaim("claims")
                .asMap();
    }

    public static void main(String[] args) {

    }
}